define(['config/appregister', 'business/login/service/http', "business/services/httpServices", "jquery"], function(app, httpServices) {
	app.controller('loginCtrl', function($scope, $state, $http, $rootScope, $stateParams,$cookies) {
		if(!angular.isUndefined($stateParams.type)) {
			if($stateParams.type=='register') {
				tabb();
			}else {
			//获取登录验证码
				coDe();			
			}
		}
		//初始值
		$scope.userInfo={name:'',pass:'',code:''};
		$scope.userTip={name:'',pass:'',all:''};
		$scope.registerInfo={name:'',pass:'',code:'',tel:'',msg:'',flag:true};
		$scope.registerTip={name:'',pass:'',code:'',tel:'',msg:'',all:''};


		//读取cookie信息
		if($cookies.getObject('userInfo')){
			$scope.userCookie=$cookies.getObject('userInfo');
			$scope.userInfo.name=$scope.userCookie.name;
			$scope.userInfo.pass=$scope.userCookie.pass;
		}
		//跳转到企业登录页
		$scope.toto = function() {
			$state.go("enterpriselogin");
		};
		//跳转到企业注册页
		$scope.totos = function() {
			$state.go("enterprisereg");
		};
		//跳转到个人忘记密码找回
		$scope.tomodpswd = function() {
			$state.go("eng.loginedBack");
		};

//		var types = sessionStorage.getItem("loginType");
//
//		if(types == 0) {
//			tabb();
//		} else if(types == 1) {
//			tabtoo();
//		}
		
		$(function(){
			$(document).keydown(function(event){
				if(event.keyCode==13){
					$(".loginbtn").click();
				}
			});
		});

		//下面是点击登录按钮同时要做的事
		$scope.dengL = function() {
			if($scope.myfs() && $scope.mypas() && $scope.checkCode()){
				var req = {
					type: 'POST',
					uri: 'personLogin',
					data: {
						"account": $scope.userInfo.name,
						"passwd":$scope.userInfo.pass,
						"verifyCode":angular.uppercase($scope.userInfo.code),
						"verifyToken":$scope.verifyToken
					}
				};
				httpServices.loginHttpService(req).success(function(response) {
					if(response.code == 0) {
						if($scope.remember){
							var data=new Date();
							data.setDate(data.getDate()+30);  //设置30天后过期
							$cookies.putObject('userInfo',{name:$scope.username,pass:$scope.password},{
								domain:'www.gongchengjie.cn',
								expires:data      //时间格式
							});
						}
						alert("登录成功");
						$scope.$emit("loginEventSuccess", {
							authToken:(response.data.authToken.split(","))[0],
							seed:(response.data.seed.split(","))[0]
						});
					} else {
						$scope.userTip.all=response.msg;
						$(".red").css("color", "red");
						coDe();
						return;
					}
				}).error(function(response) {
					$scope.userTip.all="登录失败";
					$(".red").css("color", "red");
					coDe();
				});
			}else{
				$scope.userTip.all='请正确填写登录信息';
				$(".red").css("color", "red");
			}
		};

		//点击获取短信验证码事件
		var sign = 1;
		$scope.getCodezc = function() {
			sign++;
			$scope.phonezcs();
			if(!$scope.phoneFlag){
				sign = 1;
			}else if(sign == 2) {
				var req = {
					type: 'POST',
					uri: 'personRegWithSendPhoneCode',
					data: {
						"verifyCode":angular.uppercase($scope.registerInfo.code),
						"phone": $scope.registerInfo.tel,
						"verifyToken":$scope.verifyToken
					}
				};
				httpServices.getmsgHttpService(req).success(function(res) {
					if(!res)return;
					$scope.$apply(function(){
						if(res.code == 0) {
							//$(".red5").hide();
							$('#getcode-zc').unbind("click");
							$("#getcode-zc").html("重新获取(<span id=" + 'time' + ">150</span>s)");
							var timer = $("#time").html();
							var mytime = setInterval(function() {
								if(timer > 0) {
									timer--;
									timer = timer;
								} else {
									clearInterval(mytime);
									$("#getcode-zc").html("重新获取");
									$('#getcode-zc').bind("click");
									regcode();
									sign = 1;
								}
								$("#time").html(timer);
							}, 1000)
						} else {
							alert(res.msg);
							sign = 1;
						}						
					});

				}).error(function(res) {
					alert("短信验证码请求失败");
				});
			} else {
//				regcode();
			}
		};

		//下面是点击注册按钮同时做的事
		$scope.loginZc = function() {
			if($scope.passWords() && $scope.checkRegisterCode() && $scope.checkRegisterMsg()){
				$scope.myfszc();
				$scope.phonezcs();
				if(!$scope.emailFlag){return}
				if(!$scope.phoneFlag){return}
				if(!$scope.registerInfo.flag){
					$scope.registerTip.all='请勾选已阅读并同意《工程服务条款》';
					$('.allTip').css('color','red');
					return;
				}
				console.log($scope.verifyToken,"这是第二步的");
				var req = {
					type: 'POST',
					uri: 'personRegWithBaseInfo',
					data: {
						"email": $scope.registerInfo.name,
						"phone": $scope.registerInfo.tel,
						"passwd": $scope.registerInfo.pass,
						"verifyCode": $scope.registerInfo.msg,
						"verifyToken": $scope.verifyToken
					}
				};
				httpServices.getregHttpService(req).success(function(res) {
					if(res.code == 0) {
						alert("注册成功");
						$('.allTip').css('color','green');
						tabtoo();
					} else {
						$scope.registerTip.all=res.msg;
						$('.allTip').css('color','red');
						regcode();
					};
				}).error(function(res) {
					regcode();
					alert("注册请求失败");
				});
			}
		};

		//到注册页
		function tabb() {
			$(".box").hide();
			$(".box-zc").show();
			document.getElementById("input1").value = "";
			document.getElementById("input2").value = "";
			document.getElementById("input3").value = "";
			$("#input1").blur();
			$("#input2").blur();
			$("#input3").blur();
			regcode();
		}
		//到登录页
		function tabtoo() {
			$('.box').show();
			$(".box-zc").hide();
			document.getElementById("input4").value = "";
			document.getElementById("input5").value = "";
			document.getElementById("input6").value = "";
			document.getElementById("input7").value = "";
			document.getElementById("input8").value = "";
			$("#input4").blur();
			$("#input5").blur();
			$("#input6").blur();
			$("#input7").blur();
			coDe();
		};
		$scope.Tab = function() {
			tabb();
		};
		$scope.Tabto = function() {
			tabtoo();
		};
		//点击登录页验证码图片  刷新图片
		$scope.toLoGin = function() {
			coDe();
		};
		//点击注册页验证码图片  刷新图片
		$scope.LoGin = function() {
			regcode();
		};

		//登录验证码
		function coDe() {
			var req = {
				type: 'GET',
				uri: 'personLoginWithVerifyCode',
			};
			httpServices.somefn(req).success(function(data) {
				$scope.$apply(function() {
					if(data.code == 0) {
						delCookie("verifyToken");
						$scope.imgsrc = data.data.img;
						var arr = data.data.verifyToken;
						var arred = arr.split(",");
						$scope.verifyToken = arred[0];
						var expireTime = arred[1];
						var exp = new Date();
						exp.setTime(exp.getTime() + expireTime);
						document.cookie = "verifyToken=" + $scope.verifyToken + ";expires=" + exp.toGMTString();
					} else {
						alert(data.msg);
					}
				})
			}).error(function(data) {
				alert("验证码请求失败")
			})
		};
		//个人注册验证码
		function regcode() {
			var req = {
				type: 'GET',
				uri: 'personRegWithVerifyCode',
			};
			httpServices.somefn(req).success(function(data) {
				$scope.$apply(function() {
					if(data.code == 0) {
						delCookie("verifyToken");
						$scope.imgsrcreg = data.data.img;
						var arr = data.data.verifyToken;
						var arred = arr.split(",");
						$scope.verifyToken = arred[0];
						var expireTime = arred[1];
						var exp = new Date();
						exp.setTime(exp.getTime() + expireTime);
						document.cookie = "verifyToken=" + $scope.verifyToken + ";expires=" + exp.toGMTString();
					} else {
						alert(data.msg);
					}
				})
			}).error(function(data) {
				alert("验证码请求失败")
			})
			$(document).keydown(function(event){
				if(event.keyCode==13){
					$(".loginbtn1").click();
				}
			});
		};		
		
		function delCookie(name) {
			var exp = new Date();
			exp.setTime(exp.getTime() - 1);
			var cval=getCookie(name);
			if(cval!=null)
			document.cookie= name + "="+cval+";expires="+exp.toGMTString();
		};
		
		function getCookie(name) {
			var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
			if(arr=document.cookie.match(reg))
			return unescape(arr[2]);
			else
			return null;
		};	
		
		$scope.checkRegisterCode=function(){
			if($scope.registerInfo.code==''){
				$scope.registerTip.code='验证码不能为空';
				$(".red5").css("color", "red");
			}else{
				return true;
			}
		};
		$scope.checkRegisterMsg=function(){
			if($scope.registerInfo.msg==''){
				$scope.registerTip.msg='手机验证码不能为空';
				$(".red6").css("color", "red");
			}else{
				return true;
			}
		};
		$scope.phoneFlag=false;
		$scope.phonezcs = function() {
			var regex_phone = /^1[3|4|5|7|8]\d{9}$/;
			if($scope.registerInfo.tel == "") {
				$("#input6").attr("placeholder", "请输入手机号码");
				$scope.registerTip.tel='手机号不能为空';
				$(".red4").css("color", "red");
			} else if(regex_phone.test($scope.registerInfo.tel)) {
				var req = {
					type: 'GET',
					uri: 'checkPhone',
					data: {
						phone: $scope.registerInfo.tel
					}
				};
				httpServices.checkphoneHttpService(req).success(function(res) {
					if(res.code == 0) {
						$scope.registerTip.tel="该手机号可以使用";
						$(".red4").css("color", "green");
						$scope.phoneFlag=true;
					} else {
						$scope.registerTip.tel=res.msg;
						$(".red4").css("color", "red");
					}
				}).error(function(res) {
					$scope.registerTip.tel="手机号校验请求失败";
					$(".red4").css("color", "red");
				});
			} else {
				$scope.registerTip.tel='请输入合法11位手机号';
				$(".red4").css("color", "red");
			}
		}
		$scope.passWords = function() {
			var regex_passwd = /^[\w]{6,18}$/;
			if($scope.registerInfo.pass == "") {
				$("#input5").attr("placeholder", "6-18个字符");
				$scope.registerTip.pass='密码不能为空';
				$(".red3").css("color", "red");
			} else if(regex_passwd.test($scope.registerInfo.pass)) {
				$scope.registerTip.pass='';
				return true;
			} else {
				$scope.registerTip.pass='密码为6-18个字符';
				$(".red3").css("color", "red");
			}
		};
		$scope.emailFlag=false;
		$scope.myfszc = function() {
			var regex_email = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
			if($scope.registerInfo.name == "") {
				$("#input4").attr("placeholder", "请填写正确的电子邮箱 , 用于密码找回和登录");
				$scope.registerTip.name='邮箱不能为空';
				$(".red2").css("color", "red");
			}else if(regex_email.test($scope.registerInfo.name)) {
				$scope.registerTip.name='';
				var req = {
					type: 'GET',
					uri: 'checkEmail',
					data: {
						email: $scope.registerInfo.name
					}
				};
				httpServices.checkemailHttpService(req).success(function(res) {
					if(res.code == 0) {
						$scope.registerTip.name='该邮箱可以使用';
						$(".red2").css("color", "green");
						$scope.emailFlag=true;
					} else {
						$scope.registerTip.name=res.msg;
						$(".red2").css("color", "red");
					}
				}).error(function(res) {
					$scope.registerTip.name="校验邮箱请求失败";
				});
			}else {
				$scope.registerTip.name='邮箱格式不正确';
				$(".red2").css("color", "red");
			}
		};
		$scope.checkInfo=function(){
			$(this).attr("placeholder", "");
			$scope.userTip={name:'',pass:'',all:''};
			$scope.registerTip={name:'',pass:'',code:'',tel:'',msg:'',all:''};
		};
		$scope.myfs = function() {
			var regex_phone = /^1[3|4|5|7|8]\d{9}$/;
			var regex_email =/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/;

			if($scope.userInfo.name=="") {
				$("#input1").attr("placeholder", "电子邮箱/手机号码");
				$scope.userTip.name='用户名不能为空';

			}else if(regex_phone.test($scope.userInfo.name) || regex_email.test($scope.userInfo.name)) {
				$scope.userTip.name='';
				return true;
			}else {
				$scope.userTip.name='邮箱/手机号格式错误！';
			}
		}

		$scope.mypas = function() {
			var regex_password = /^[\w]{6,16}$/;
			if($scope.userInfo.pass == "") {
				$(".password").attr("placeholder","6-18个字符");
				$scope.userTip.pass='密码不能为空';
			} else if((regex_password.test($scope.userInfo.pass))) {
				$scope.userTip.pass='';
				return true;
			} else {
				$scope.userTip.pass='密码错误！';
			}
		};
		$scope.checkCode=function(){
			if($scope.userInfo.code==''){
				$scope.userTip.all='验证码不能为空';
			}else{
				return true;
			}
		};
		//QQ第三方登录
		var otherUrl = "http://"+allUrl+":8080/";
		var otherAPI = 'elink_user/api/oauth2';

		function otherLogin() {
			$http({
				method: 'GET',
				url: otherUrl + otherAPI + '/getUrl'
			}).success(function(response) {
				$scope.QQ = response.data.QQ;
				$scope.Weixin = response.data.Weixin;
				$scope.Sina = response.data.Sina;
//				console.log(response.data.Weixin)
			})
		}

		//微信
		/*var doc = document.getElementsByTagName("head");
		var scr = document.createElement('script');
		scr.src = "https://res.wx.qq.com/connect/zh_CN/htmledition/js/wxLogin.js";
		doc[0].appendChild(scr);
	
		function wxlogin(){
			var obj = new Wxlogin({
				id:'login_container',
				appid:"wxd22274cae2b548d3",
				scope:"snsapi_login#wechat_redirect",
				redirect_uri:"http://"+allUrl+":8082/elink_common/getRecall",
				state:"f7548f24c5589004ebf982377a160a94",
				style:"",
				href:""
			});
		}
		setTimeout(wxlogin,1000)*/

		/*function getWechat(){
			require(["weixin"],function(Wxlogin){
							
		
			})
		}
		getWechat()*/

		otherLogin();

		//下面是刷新页面重制验证码的动作

	})

})